package com.heima.wemedia.v1.controller;

import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.model.admin.dtos.NewsAuthDelDto;
import com.heima.model.media.dtos.WmDownOrUpDTO;
import com.heima.model.media.dtos.WmNewsDto;
import com.heima.model.media.dtos.WmNewsPageReqDto;
import com.heima.wemedia.entity.WmNews;
import com.heima.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;

@RestController
public class WmNewsController {

    @Autowired
    private WmNewsService newsService;


    /**
     * feign调用，媒体审核通过
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/news_auth/auth_pass")
    void authPass(@RequestBody NewsAuthDelDto dto) throws InvocationTargetException, IllegalAccessException {
        Integer id = dto.getId();
        newsService.authPass(id);


    }

    /**
     * fegit接口调用 媒体人工审核不通过
     * @param dto
     */
    @PostMapping("/api/v1/news_auth/auth_fail")
    void authFail(@RequestBody NewsAuthDelDto dto){
        newsService.authFail(dto);
    }

    /**
     * 文章的草稿 提交审核 编辑
     *
     */
    @PostMapping("/api/v1/news/submit")
    public ResponseResult submit(@RequestBody WmNewsDto dto){
        newsService.submit(dto);
        return ResponseResult.ok();
    }

    /**
     * 分页查询
     * */
    @PostMapping("/api/v1/news/page")
    public PageResult<WmNews> pageQuery(@RequestBody WmNewsPageReqDto dto){
        return newsService.pageQuery(dto);
    }

    @GetMapping("/api/v1/news/one/{id}")
    public ResponseResult<WmNews> findById(@PathVariable("id")Integer id){
        WmNews wmNews = newsService.getById(id);
        if(wmNews == null){
            return ResponseResult.error(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.ok(wmNews);
    }

    @PutMapping("/api/v1/news/down_or_up")
    public ResponseResult downOrUp(@RequestBody WmDownOrUpDTO dto){
        newsService.downOrUp(dto);
        return ResponseResult.ok();
    }

}
